<!-- 日志管理 -->
<template>
  <div class="app-container">
    <el-row :gutter="24">
      <!-- 日志列表 -->
      <el-col :lg="24" :xs="24">
        <!-- 搜索区域 -->
        <div class="search-container">
          <el-form
            ref="queryFormRef"
            :model="queryParams"
            :inline="true"
            label-width="auto"
          >
            <el-form-item :label="$t('Keyword')" prop="keywords">
              <el-input
                v-model="queryParams.keywords"
                :placeholder="$t('Please_Enter') + ' ' + $t('Keyword')"
                clearable
              />
            </el-form-item>
            <el-form-item class="search-buttons">
              <el-button type="primary" icon="search" @click="handleQuery">
                {{ $t("Search") }}
              </el-button>
              <el-button icon="refresh" @click="handleResetQuery">
                {{ $t("Reset") }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="hover" class="data-table">
          <!-- <div class="data-table__toolbar" style="margin-bottom: 10px">
            <div class="data-table__toolbar--actions">
              <el-button
                v-hasPerm="'authHandleLogDelBtn'"
                type="danger"
                icon="delete"
                :disabled="selectIds.length === 0"
                @click="handleDelete()"
              >
                {{ $t("Delete") }}
              </el-button>
            </div>
          </div> -->

          <el-table
            v-loading="loading"
            :data="pageData"
            :selectable="selectable"
            border
            stripe
            highlight-current-row
            class="data-table__content"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column
              :label="$t('Administrator') + 'id'"
              prop="admin_id"
              width="100"
            />
            <el-table-column
              :label="$t('Administrator') + $t('Name')"
              prop="admin_name"
            />
            <el-table-column
              :label="$t('Administrator') + $t('Router')"
              prop="url"
            />
            <el-table-column :label="$t('Operate') + $t('Name')" prop="name" />
            <el-table-column
              :label="$t('Create_Time')"
              align="center"
              prop="created_at"
              width="200"
            />
            <el-table-column
              :label="$t('Update_Time')"
              align="center"
              prop="updated_at"
              width="200"
            />
            <el-table-column
              :label="$t('Operate')"
              fixed="right"
              align="center"
            >
              <template #default="scope">
                <el-button
                  v-hasPerm="'authHandleLogEdit'"
                  type="primary"
                  icon="edit"
                  link
                  size="small"
                  @click="handleOpenDialog(scope.row)"
                >
                  {{ $t("View") }}
                </el-button>
                <!-- <el-button
                  v-hasPerm="'authHandleLogDelBtn'"
                  type="danger"
                  icon="delete"
                  link
                  size="small"
                  @click="handleDelete(scope.row.id)"
                >
                  {{ $t("Delete") }}
                </el-button> -->
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            v-model:current-page="queryParams.page"
            v-model:page-size="queryParams.pageSize"
            :page-sizes="[15, 20, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleQuery"
            @current-change="handleQuery"
          />
        </el-card>
      </el-col>
    </el-row>
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="1000"
      :before-close="handleCloseDialog"
      :align-center="true"
      :close-on-click-modal="false"
    >
      <el-form ref="userFormRef" :model="formData" label-width="130px">
        <el-form-item :label="$t('Administrator')">
          <label>{{ formData.admin_name }} ({{ formData.admin_id }})</label>
        </el-form-item>
        <el-form-item :label="$t('Operate') + $t('Name')">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item :label="$t('Operate') + $t('Router')">
          <el-input v-model="formData.url" />
        </el-form-item>
        <el-form-item :label="$t('Data')">
          <el-input :rows="10" type="textarea" v-model="formData.param" />
        </el-form-item>
        <el-form-item :label="$t('Create_Time')">
          <el-input v-model="formData.created_at" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleCloseDialog">{{ $t("Cancel") }}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useAppStore } from "@/store/modules/app";
import { DeviceEnum } from "@/enums/DeviceEnum";
import i18n from "@/lang/index";

import LogAPI, {
  HandleLogInfo,
  HandleLogInfoListVO,
  HandleLogPageQuery,
  HandleLogInfoList,
} from "@/api/system/log.api";

defineOptions({
  name: "AuthHandleLog",
  inheritAttrs: false,
});

const appStore = useAppStore();

const queryFormRef = ref();
const userFormRef = ref();

const queryParams = reactive<HandleLogPageQuery>({
  page: 1,
  pageSize: 15,
});
const selectable = (row: HandleLogInfo) => ![1].includes(Number(row.id));
const pageData = ref<HandleLogInfoList[]>();
const total = ref(0);
const loading = ref(false);

const dialog = reactive({
  visible: false,
  title: i18n.global.t("View") + i18n.global.t("Log"),
});
const drawerSize = computed(() =>
  appStore.device === DeviceEnum.DESKTOP ? "600px" : "90%"
);

let formData = reactive<HandleLogInfo>({
  id: "",
  admin_id: "",
  url: "",
  param: "",
  created_at: "",
});

const rules = reactive({
  name: [{ required: true, message: "操作名称", trigger: "blur" }],
  url: [{ required: true, message: "路由地址", trigger: "blur" }],
});

// 选中的日志ID
const selectIds = ref<number[]>([]);
// 角色下拉数据源
const groupOptions = ref<HandleLogInfoList[]>();
// 导入弹窗显示状态
const importDialogVisible = ref(false);

// 查询
async function handleQuery() {
  loading.value = true;
  LogAPI.getPage(queryParams)
    .then((data: HandleLogInfoListVO) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
// 重置查询
function handleResetQuery() {
  queryParams.page = 1;
  queryParams.keywords = "";
  handleQuery();
}

// 选中项发生变化
function handleSelectionChange(selection: any[]) {
  selectIds.value = selection.map((item) => item.id);
}

/**
 * 打开弹窗
 *
 * @param id 日志ID
 */
async function handleOpenDialog(user?: HandleLogInfo) {
  dialog.visible = true;
  if (user?.id) {
    dialog.title = "查看日志";
    //复制 user 给formData.value
    Object.assign(formData, user);
  } else {
    dialog.title = "新增日志";
  }
}

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  userFormRef.value.resetFields();
  userFormRef.value.clearValidate();
}

/**
 * 删除日志
 *
 * @param id  日志ID
 */
function handleDelete(id?: number) {
  const userIds = [id || selectIds.value].join(",");
  if (!userIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除日志?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    function () {
      loading.value = true;
      console.log(userIds);
      LogAPI.deleteByIds({ ids: userIds })
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    function () {
      ElMessage.info("已取消删除");
    }
  );
}

onMounted(() => {
  handleQuery();
});
</script>
